//
//  FunDataHTTP.m
//  USB_Reader
//
//  Created by xiaozao on 15/3/23.
//  Copyright (c) 2015年 Tony_Wang. All rights reserved.
//

#import "FunDataHTTP.h"
#import "AFNetworking.h"
@implementation FunDataHTTP



+(void) requsetWithURL:(NSString *)urlStr cache:(NSString *)cachePath completion:(void(^)(NSData *resultData))block
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //指定response
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // 增加缓存功能
//    NSArray *dataPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//    NSString *dataPath = [dataPaths lastObject];
    
    NSString *cache = [NSString stringWithFormat:@"%@",cachePath];
    
    NSString *path1 = [[NSBundle mainBundle] pathForResource: cache ofType:@""];
//    NSLog(@"%@ \n %@", path1, dataPath);
//    NSString *path = [dataPath stringByAppendingPathComponent:cachePath];
    
    

    
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = responseObject;

        
        [NSKeyedArchiver archiveRootObject:data toFile:path1];
        block(data);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSMutableData *myData = [NSKeyedUnarchiver unarchiveObjectWithFile:path1];
    block(myData);
    }];
}



@end
